import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:testflutte/shuqi/bookshelf_cene.dart';
import 'package:testflutte/shuqi/global.dart';
import 'package:testflutte/shuqi/home_cene.dart';
import 'package:testflutte/shuqi/my_scene.dart';

class RotScene extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => RotSceneState();
}

class RotSceneState extends State<RotScene> {
  int _tabIndex = 0; //当前页数
  bool isFinishSetup = false; //
  List<Image> _tabImages = [
    Image.asset('images/tab_bookshelf_n.png'),
    Image.asset('images/tab_bookstore_n.png'),
    Image.asset('images/tab_me_n.png'),
  ];
  List<Image> _tabSelectedImages = [
    Image.asset('images/tab_bookshelf_p.png'),
    Image.asset('images/tab_bookstore_p.png'),
    Image.asset('images/tab_me_p.png'),
  ];

  @override
  void initState() {
    //初始化
    super.initState();
    setupApp();
  }

  setupApp() async {
    preferences = await SharedPreferences.getInstance();
    setState(() {
      isFinishSetup = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        children: <Widget>[
          //页面叠加,有点像Fragment
          BookshelfScene(),
          HomeScene(),
          MyScene()
        ],
        index: _tabIndex,
      ),
      bottomNavigationBar: CupertinoTabBar(
        backgroundColor: Colors.white,
        activeColor: Colors.redAccent,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: getTabIcon(0), title: Text('书架')),
          BottomNavigationBarItem(icon: getTabIcon(1), title: Text('书城')),
          BottomNavigationBarItem(icon: getTabIcon(2), title: Text('我的')),
        ],
        currentIndex: _tabIndex,
        onTap: (index){
          setState(() {
            _tabIndex = index;
          });
        },
      ),
    );
  }

  Image getTabIcon(int index) {
    if (index == _tabIndex) {
      return _tabSelectedImages[index];
    } else {
      return _tabImages[index];
    }
  }
}
